package com.service;

import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_BOX;
import static com.def.GenericDef.E_XPREWORK_BOX_STATUS_INVALIED;
import static com.def.GenericDef.E_XPREWORK_INVALID_INPUT;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPREWORK;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisMdlDefDao;
import com.dao.IBisOpeDao;
import com.dao.IBisPathDao;
import com.dao.IBisPathiDao;
import com.dao.IBisToolDao;
import com.dao.IBisToolGrDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdOutDao;
import com.dao.IRetToolStDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_box;
import com.rtnBean.NotFoundRtnBeen;
import com.tx.Xprework.XpreworkI;
import com.tx.Xprework.XpreworkO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpreworkService")
public class XpreworkService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpreworkService.class);
	private static String TX_NAME = T_XPREWORK;
	
	private XpreworkI inTrx ;
	private XpreworkO outTrx ;
	private MqOpe mqOpe;
	
	private String gszMsgBuf = "";	
	private Timestamp gEvtTimestamp;
	private String gEvtSeqID;
	
	
	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	@Autowired
	private IRetToolStDao retToolStDao;	
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;	
	@Autowired
	private IBisOpeDao bisOpeDao;	
	@Autowired
	private IBisPathDao bisPathDao;	
	@Autowired
	private IBisPathiDao bisPathiDao;	
	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	
	public String  subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;
		
		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XpreworkI();
		outTrx = new XpreworkO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();
		
    	try{
    		inTrx = (XpreworkI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);
			
			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
    	}catch(Exception ex){
    		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    		outTrx.setRtn_mesg(ex.toString());
    		outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
    		logger.error(Util.stackTraceToString(ex));
    	}finally{
    		outMsg = mqOpe.obj2Str(outTrx);
    		return outMsg; 
    	}
	}
	
	
	/**
	 * All move in logic
	 * @return
	 */
	private long subMainProc2(char actionType){
		long lRc = _NORMAL;
		
		switch (actionType) {
		case 'I': {
			lRc = reworkIn();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'C': {
			lRc = rewoekInCancel();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		default: {
			gszMsgBuf = "Invalide type input: [" + actionType + "]";
			return E_XPREWORK_INVALID_INPUT;
		}
		}

		return _NORMAL;
	}


	private long rewoekInCancel() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * Rework in
	 * @return
	 */
	private long reworkIn() {
		long lRc = _NORMAL;
		
		lRc = getRetBoxInfo(inTrx.getBox_id());
		if(_NORMAL != lRc){
			return lRc;
		}
		
		return _NORMAL;
	}
	
	/**
	 * Get box info
	 * @return
	 */
	private long getRetBoxInfo(String boxID) {
		long rtnCode = _NORMAL;
		Ret_box ret_box = new Ret_box();
		
		ret_box = retBoxDao.get(boxID);
		if (null == ret_box) {
			gszMsgBuf = new NotFoundRtnBeen(boxID, Ret_box.class).toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}
		
		// Check status
		if (!PROC_WAIT.equals(ret_box.getBox_stat())) {
			gszMsgBuf = "Box [" + boxID + "] is invalied status ["
					+ ret_box.getBox_stat() + "], must [" + PROC_WAIT + "]!";
			return E_XPREWORK_BOX_STATUS_INVALIED;
		}
		
		return _NORMAL;
	}

}
